﻿using LibBinary.DataSource;
using LibBinary.FileData;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LibBinary.FileDeclaration.Binary
{
    public class SimpleFieldArrangement : IFieldArrangement
    {
        public AbstractFileData Read(BinaryFileSource src, AbstractFileData parentNode, string name, IFileDeclaration decl)
        {
            var ret = new FieldData(parentNode);
            ret.Data = decl.Read(src, ret);
            ret.FieldName = name;
            return ret;
        }

        public void Write(BinaryFileSource src, AbstractFileData data, string name, IFileDeclaration decl)
        {
            var cdata = data.As<FieldData>();
            if (cdata == null)
            {
                throw new WritingFormatException("Wrong data type.");
            }
            if (cdata.FieldName != name)
            {
                throw new WritingFormatException("Field name mismatch.");
            }
            decl.Write(src, cdata.Data);
        }

        private static SimpleFieldArrangement _Instance = new SimpleFieldArrangement();
        public static SimpleFieldArrangement Instance
        {
            get
            {
                return _Instance;
            }
        }
    }
}
